﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using it_security_project.App_Code.BusinessLogicLayer;
using System.Web.Configuration;
using System.Configuration;
//using SecurePages.Infrastructure;

namespace it_security_project
{
    public partial class Site : System.Web.UI.MasterPage
    {
        private String m_sessionUsernameString = "Username";
        private String m_sessionPasswordString = "Password";
        private String m_sessionIPString = "IP";
        private String m_sessionRoleString = "Role";
        private Service service = new Service();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //Configuration config = WebConfigurationManager.OpenWebConfiguration("~");

            //ConnectionStringsSection connSection = (ConnectionStringsSection)config.GetSection("connectionStrings");

            //connSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

            //config.Save();
        }



        protected override void OnInit(EventArgs e)
        {
            //SecurePagesConfiguration.Urls.AddUrl("/Login");
            //SecurePagesConfiguration.Urls.AddUrl("/login");

            /// <summary>
            /// Check active URL and set class on navigation.
            /// </summary>
            String pageName = Request.FilePath;
            String activeClassName = "here";
            String checkAttribute = "href";
            String changeAttribute = "class";

            // See if user is able to login with session
            Member member = LoginWithSession();

            if (member == null) // User is NOT logged in
            {
                // Logged out user
                profilesite.Visible = false;
                logoutsite.Visible = false;
                loginsite.Visible = true;
                registersite.Visible = true;
            }
            else
            {
                profilesite.Visible = true;
                logoutsite.Visible = true;
                loginsite.Visible = false;
                registersite.Visible = false;                
                

                // User is logged in, check role
                if (member.Role == Roles.Member) // user is member
                {
                    
                }
                else if (member.Role == Roles.Admin) // User is Admin
                {
                    
                }
                else if (member.Role == Roles.Moderator) // User is moderator
                {
                    
                }
            }

            // Check active link
            if (pageName.ToLower() == "/" + defsite.Attributes[checkAttribute].ToLower())
            {
                defsite.Attributes[changeAttribute] = activeClassName;
            }
            else if (pageName.ToLower() == "/" + aboutsite.Attributes[checkAttribute].ToLower())
            {
                aboutsite.Attributes[changeAttribute] = activeClassName;
            }
            else if (pageName.ToLower() == "/" + forumsite.Attributes[checkAttribute].ToLower())
            {
                forumsite.Attributes[changeAttribute] = activeClassName;
            }
            else if (pageName.ToLower() == "/" + profilesite.Attributes[checkAttribute].ToLower())
            {
                profilesite.Attributes[changeAttribute] = activeClassName;
            }
            else if (pageName.ToLower() == "/" + registersite.Attributes[checkAttribute].ToLower())
            {
                registersite.Attributes[changeAttribute] = activeClassName;
            }
            else if (pageName.ToLower() == "/" + loginsite.Attributes[checkAttribute].ToLower())
            {
                loginsite.Attributes[changeAttribute] = activeClassName;
            }
            else if (pageName.ToLower() == "/" + logoutsite.Attributes[checkAttribute].ToLower())
            {
                logoutsite.Attributes[changeAttribute] = activeClassName;
                ClearSession();
            }
            


            // Set message to user.
            if (Session["NameOfUser"] != null)
            {
                NameLabel.Text = "Welcome: " + Session["NameOfUser"].ToString();
            }
            else
            {
                if (pageName.ToLower() != "/" + loginsite.Attributes[checkAttribute].ToLower())
                {
                    //NameLabel.Text = "Please <a href='" + loginsite.Attributes[checkAttribute].ToLower() + "'>Login.</a>";    
                }                
            }
        }

        /// <summary>
        /// Returns a member object if username and password is correct.
        /// </summary>
        /// <returns>Member</returns>
        internal Member LoginUserWithInput(String a_username, String a_password)
        { 
            var member = service.GetUserPasswordOnUser(a_username);

            if (member != null)
            {
                string crypto = Cryptation.HashPassword(a_password);
                if (crypto == member.Password)
                {
                    return member;
                }
            }

            return null;
        }

        /// <summary>
        /// Returns a member obj if login with session is successful.
        /// </summary>
        /// <returns>Member</returns>
        internal Member LoginWithSession()
        {
            if (Session["NameOfUser"] != null && Session["Password"] != null)
            {
                string username = Session["NameOfUser"].ToString();
                string password = Session["Password"].ToString();
                Member member = service.GetUserPasswordOnUser(username);

                if (member != null)
                {
                    if (password == member.Password)
                    {
                        return member;
                    }
                }

            }
            return null;
        }

        /// <summary>
        /// CLears the session
        /// </summary>
        internal void ClearSession()
        { 
            Session["NameOfUser"] = null;
            Session["Password"] = null;
        }

        /// <summary>
        /// Returns a member ID with Session. If error returns -1
        /// </summary>
        internal int GetMemberID()
        {
            if (Session["NameOfUser"] != null)
            {
                Member member = service.GetUserPasswordOnUser(Session["NameOfUser"].ToString());
                if (member != null)
                {
                    return member.MemberID;
                }
            }
            return -1;
        }


        /// <summary>
        /// Returns a member name with Session. If error returns null
        /// </summary>
        internal string GetMemberName()
        {
            if (Session["NameOfUser"] != null)
            {
                Member member = service.GetUserPasswordOnUser(Session["NameOfUser"].ToString());
                if (member != null)
                {
                    return member.Username;
                }
            }
            return null;
        }

        internal string GetSessionUsername()
        {
            return Session["NameOfUser"].ToString();
        }

        internal void SetViewstate(Member member)
        {
            Session["NameOfUser"] = member.Username;
            Session["Password"] = member.Password;
            //NameLabel.Text = member.Username;
        }

        internal void ChangeCustomMessage(string message, MessageType csstype)
        {
            LabelCustomMessage.Text = message;
            LabelCustomMessage.CssClass = csstype.ToString();
        }
    }
}